CONTENTS | INDEX | PREV | NEXT
rename
NAME
rename - rename a file, also is able to move a file from one
directory to another on the same filesystem.
SYNOPSIS
#include <stdio.h>
int error = rename(origname, newname);
const char *origname;
const char *newname;
FUNCTION
rename() renames a file. You may also use rename() to move a file
(& rename at the same time) to another directory on the same
filesystem.
rename returns 0 on success, a negative number on failure.
EXAMPLE
/*
* create the file T:xxjunk and the directory T:junkdir then
* move T:xxjunk into T:junkdir and rename to T:yyjunk at the
* same time.
*
* As is aptly demonstrated by this example, some errors are not
* really errors. For example, mkdir where the dir already exists
* is not usually an error.
*/
#include <stdio.h>
#include <assert.h>
#include <errno.h>
main()
{
FILE *fp;
int error;
error = mkdir("T:junkdir");
if (error < 0 && errno != EEXISTS)
perror("mkdir");
fp = fopen("T:xxjunk", "w");
if (fp == NULL) {
perror("fopen");
exit(1);
}
fprintf(fp, "This file was originally T:xxjunk!n");
fclose(fp);
/*
* now rename
*/
error = rename("T:xxjunk", "T:junkdir/yyjunk");
if (error < 0)
perror("rename T:xxjunk to T:junkdir/yyjunk");
else
puts("rename succeeded, look in T:junkdir");
return(0);
}
INPUTS
const char *origname; existing file
const char *newname; new filename and/or path
RESULTS
int error; 0 on success, a negative number on failure
SEE ALSO